package com.yu1996.gulimall.ware.controller;

import com.yu1996.gulimall.ware.vo.MergeVo;
import com.yu1996.gulimall.ware.vo.PurchaseDoneVo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yu1996.gulimall.ware.entity.PurchaseEntity;
import com.yu1996.gulimall.ware.service.PurchaseService;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.R;

/**
 * 采购信息
 *
 * @author yu
 * @email 1798121567@qq.com
 * @date 2021-08-06 15:40:02
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
  @Autowired private PurchaseService purchaseService;
  /** 完成采购单 */
  @PostMapping("/done")
  // @RequiresPermissions("ware:purchase:list")
  public R finish(@RequestBody PurchaseDoneVo vo) {
    purchaseService.done(vo);

    return R.ok();
  }

  /** 领取采购单 */
  @PostMapping("/received")
  // @RequiresPermissions("ware:purchase:list")
  public R received(@RequestBody List<Long> ids) {
    purchaseService.received(ids);

    return R.ok();
  }

  /** 合并采购需求 */
  @PostMapping("/merge")
  // @RequiresPermissions("ware:purchase:list")
  public R merge(@RequestBody MergeVo mergeVo) {
    purchaseService.mergePurchase(mergeVo);

    return R.ok();
  }

  /** 查询新建或已分配采购单的列表 */
  @RequestMapping("/unreceive/list")
  // @RequiresPermissions("ware:purchase:list")
  public R unreceivePurchase(@RequestParam Map<String, Object> params) {
    PageUtils page = purchaseService.unreceivePurchase(params);

    return R.ok().put("page", page);
  }
  /** 列表 */
  @RequestMapping("/list")
  // @RequiresPermissions("ware:purchase:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = purchaseService.queryPage(params);

    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{id}")
  // @RequiresPermissions("ware:purchase:info")
  public R info(@PathVariable("id") Long id) {
    PurchaseEntity purchase = purchaseService.getById(id);

    return R.ok().put("purchase", purchase);
  }

  /** 保存 */
  @RequestMapping("/save")
  // @RequiresPermissions("ware:purchase:save")
  public R save(@RequestBody PurchaseEntity purchase) {
    purchaseService.save(purchase);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  // @RequiresPermissions("ware:purchase:update")
  public R update(@RequestBody PurchaseEntity purchase) {
    purchaseService.updateById(purchase);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  // @RequiresPermissions("ware:purchase:delete")
  public R delete(@RequestBody Long[] ids) {
    purchaseService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }
}
